package at.jku.pervasive.simulator;

import java.io.IOException;

import at.jku.pervasive.Measurement;

public class StepwiseFileServerSimulator extends FileServerSimulator
{

	private boolean isPlaying;
	private PositionBuffer posBuffer;

	public StepwiseFileServerSimulator(int port) throws IOException
	{
		super(port, null);
		posBuffer = new PositionBuffer();
	}

	public void setMeasurement(Measurement measurement)
	{
		this.measurement = measurement;
		this.positions = this.measurement.getPositions();

		isPlaying = false;
		posBuffer.setPosition(null);
	}

	public void play()
	{
		isPlaying = true;
		posBuffer.setPosition(null);
	}

	public void step()
	{
		isPlaying = true;
		posBuffer.setPosition(null);
		isPlaying = false;
	}

	public void pause()
	{
		isPlaying = false;
	}

	@Override
	protected long getWaitingTime()
	{
		if (!isPlaying)
		{
			posBuffer.get();
			return 0;
		}

		return super.getWaitingTime();
	}

	public int getCurrentPosition()
	{
		return this.currentPosition;
	}

	public void step(int currentValue)
	{
		this.currentPosition = currentValue;
		step();
	}

	public boolean isPlaying()
	{
		return isPlaying;
	}

}
